/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.gestion;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.gestion.exception.BodegaException;
import co.gov.medellin.sdep.sisdep.gestion.exception.BodegaRuntimeException;
import co.gov.medellin.sdep.sisdep.gestion.service.IBodegaService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.model.Bodega;

/**
 * @author Usuario
 * 
 */
public class BodegaController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public BodegaController() {
		cleanSearchValues();
		cleanValues();
	}

	/** bean para el manejo del formulario de busqueda Bodega */
	private Bodega bodegaSearch = new Bodega();

	/** Objeto contenedor del listado de Bodega luego de una consulta. */
	private DataModel bodegaListModel;

	/** bean para el manejo del formulario de creacion/edicion. Bodega */
	private Bodega bodega = new Bodega();

	/** instancia de servicio bodegaService */
	private IBodegaService bodegaService;

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchBodegaByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getBodegaService().load(1, 10, this.bodegaSearch);
			this.bodegaListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.bodegaListModel.getWrappedData() == null) {
			if(msg.equals("")){
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		} else {
			if (((List) this.bodegaListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.BODEGA_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.bodega.getId().longValue() == 0L) {
				getBodegaService().insert(this.bodega);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (BodegaException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (BodegaRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		cleanValues();
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Bodega " + msg, null, null);
		return AppConstants.BODEGA_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.bodega.getId().longValue() != 0L) {
				getBodegaService().update(this.bodega);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (BodegaException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (BodegaRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		cleanValues();
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Bodega " + msg, null, null);
		return AppConstants.BODEGA_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String initState() {
		cleanSearchValues();
		cleanValues();
		return AppConstants.EST_BODEGA_INIT_VIEW;
	}

	/**
	 * 
	 */
	public String createBodega() {
		bodega = new Bodega();
		cleanValues();
		return AppConstants.BODEGA_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateBodega() {
		Map params = getExternalContext().getRequestParameterMap();
		String bodegaDataIndex = (String) params.get("bodegaDataIndex");
		this.bodega = (Bodega) ((List) this.bodegaListModel.getWrappedData()).get(Integer.parseInt(bodegaDataIndex));
		cleanSearchValues();
		return AppConstants.BODEGA_UPDATE_VIEW;
	}

	/**
	 *
	 * @return
	 */
	public String cancelBodega() {
		cleanSearchValues();
		return AppConstants.BODEGA_SEARCH_VIEW;
	}

	/**
	 *
	 * @return
	 */
	public String cancelBodegaOpcion() {
		cleanValues();
		cleanSearchValues();
		return AppConstants.BODEGA_SEARCH_VIEW;
	}

	/**
	 * limpia el contenido de los valores que se ingresan para buscar
	 */
	public final void cleanSearchValues(){
		bodegaSearch.setCodigo("");
		bodegaSearch.setTipo("S");
		bodegaSearch.setClase("");
		bodegaSearch.setNombre("");
		bodegaSearch.setEstado("");
	}

	/**
	 * limpia el contenido de los valores que se ingresan (Create, Update)
	 */
	public final void cleanValues(){
		bodega.setId(new Long(0L));
		bodega.setCodigo("");
		bodega.setTipo("S");
		bodega.setClase("");
		bodega.setNombre("");
		bodega.setEstado("");
		this.bodegaListModel = new ListDataModel();
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public Bodega getBodegaSearch() {
		return bodegaSearch;
	}

	/**
	 * 
	 * @param bodegaSearch
	 */
	public void setbodegaSearch(Bodega bodegaSearch) {
		this.bodegaSearch = bodegaSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getbodegaListModel() {
		return bodegaListModel;
	}

	/**
	 * 
	 * @param bodegaListModel
	 */
	public void setbodegaListModel(DataModel bodegaListModel) {
		this.bodegaListModel = bodegaListModel;
	}

	/**
	 * 
	 * @return
	 */
	public Bodega getBodega() {
		return bodega;
	}

	/**
	 * 
	 * @param bodega
	 */
	public void setbodega(Bodega bodega) {
		this.bodega = bodega;
	}

	/**
	 * obtencion del servicio bodegaService
	 * 
	 * @return
	 */
	public IBodegaService getBodegaService() {
		return bodegaService;
	}

	/**
	 * establecimiento del servicio bodegaService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param bodegaService
	 */
	public void setBodegaService(
			IBodegaService bodegaService) {
		this.bodegaService = bodegaService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
